﻿
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using WPFServices;
using Wpf2.Models;
using WPFServices.Services;
using MessageBox = System.Windows.MessageBox;

namespace Wpf2.ViewModels
{
    public class ViewModelUser
    {
        private ObservableCollection<User> _userList;
       
        public  ViewModelUser()
        {
            _userList = new ObservableCollection<User>();
            var temp = UserService.GetListUser().Select(x => new User
                                                                 {
                                                                     UserIdAuto = x.ID_USER_AUTO,
                                                                     UserId = x.ID_USER??0,
                                                                     UserName = x.USERNAME,
                                                                     Password = x.PASSWORDS
                                                                 });
            foreach (var user in temp)
            {
                _userList.Add(user);
            }


            
        }
        public ObservableCollection<User> ListUsers
        {
            get { return _userList; }
            set { _userList = value; }
        }

        public void DtgCellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
            var c = e.Row.Item as User;
            if (e.Column.DisplayIndex != 1) return;
            if (c == null) return;
            var id = c.UserId;
            var listUser = UserService.GetListUser();
            var action = _userList.Where(x => x.UserIdAuto == c.UserIdAuto).Select(x => x.UserIdAuto).SingleOrDefault();
            if(action==0)
            {
                
                if (listUser.All(x => x.ID_USER != id)) return;
                MessageBox.Show("Mã người dùng đã tồn tại.");
                c.UserId = 0;
            }
            else
            {
                var currentC = UserService.GetUser(c.UserIdAuto);
                if (currentC.ID_USER== c.UserId) return;

                if (listUser.All(x => x.ID_USER != id)) return;
                MessageBox.Show("Mã người dùng đã tồn tại.");
                c.UserId = 0;
            }

            
        }

        public void DtgRowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            if (e.EditAction != DataGridEditAction.Commit) return;
            var c = e.Row.DataContext as User;
            if (c == null || c.UserId == 0 || c.Password == null)
            {
                return;
            }
            var action = _userList.Where(x =>  x.UserIdAuto == c.UserIdAuto).Select(x => x.UserIdAuto).SingleOrDefault();
            if (action == 0)
            {
                    var us = new USER
                    {
                        ID_USER = c.UserId,
                        USERNAME = c.UserName,
                        PASSWORDS = c.Password,
                    };

                    var idUs = UserService.CreateUser(us);
                    var firstOrDefault = _userList.FirstOrDefault(x => x.UserIdAuto == 0);
                    if (firstOrDefault != null)
                        firstOrDefault.UserIdAuto = idUs;
            }
            else
            {
              var currentC = UserService.GetUser(c.UserIdAuto);
              if (currentC == null) return;
                    currentC.ID_USER = c.UserId;
                currentC.USERNAME = c.UserName;
                    currentC.PASSWORDS = c.Password;
                    UserService.UpdateUser(currentC);
                
            }
        }

    }
}
